package com.kuaixueit.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaixueit.crm.mapper.CRoleFunctionMapper;
import com.kuaixueit.crm.mapper.CRoleMapper;
import com.kuaixueit.crm.pojo.CRole;
import com.kuaixueit.crm.service.RoleService;
@Service
public class RoleServiceImpl implements RoleService {

	
	@Autowired
	private CRoleMapper roleMapper;
	
	@Autowired
	private CRoleFunctionMapper roleFunctionMapper;
	
	@Override
	public int deleteByPrimaryKey(Integer id) {
		//删除角色同时删除角色拥有的权限
		roleFunctionMapper.deleteByRoleId(id);
		return roleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(CRole record) {
		record.setId(null);
		return roleMapper.insert(record);
	}

	@Override
	public int insertSelective(CRole record) {
		return roleMapper.insertSelective(record);
	}

	@Override
	public CRole selectByPrimaryKey(Integer id) {
		return roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(CRole record) {
		return roleMapper.updateByPrimaryKey(record);
	}

	@Override
	public int updateByPrimaryKey(CRole record) {
		return roleMapper.updateByPrimaryKey(record);
	}

	@Override
	public PageInfo findPageRole(Integer page, Integer rows) {
		PageHelper.startPage(page, rows);
		List<CRole> list = roleMapper.findPage();
		PageInfo info = new PageInfo(list);
		return info;
	}

	@Override
	public List findFunctionByRoleId(Integer roleId) {
		return roleMapper.findFunctionByRoleId(roleId);
	}


	//业务层的方法
	@Override
	public void deleteFunctionByRoid(Integer roleId) {
		//数据层的方法
		roleMapper.deleteFunctionByRoid(roleId);
	}

	@Override
	public void insertFunnctionsByRoid(Integer roleId, Integer[] fid) {
		//先删除role的全新啊
		//再增加role的权限
		//1。调用业务层方法 使用事物的传播机制
		//2。调用数据层方法 不适用事物的传播机制
		roleMapper.deleteFunctionByRoid(roleId);
		roleMapper.insertFunnctionsByRoid(roleId, fid);
		
	}

}
